﻿using System;
using UnityEngine;
using Network;

public class LogicMoveComponent : LogicComponent
{
    Logic lEntity;

    CharacterController characterController;

    Vector3 curMove;

    public virtual void DoUpdate(float deltaTime)
    {
        characterController.Move((curMove + Physics.gravity) * deltaTime);
        curMove = Vector3.zero;
        Vector3 result = characterController.transform.position;
        //send move_move operation
        move_move proto = new move_move();
        proto.pos = new vector3();
        proto.pos.x = result.x; proto.pos.y = result.y; proto.pos.z = result.z;
        lEntity.SendEntityMessage(OpCode.move_move, proto);
    }

    public virtual void Init(Logic LEntity)
    {
        lEntity = LEntity;
        GameObject objController = (GameObject)GameObject.Instantiate(Resources.Load("Prefabs/CharacterController"));
        characterController = objController.GetComponent<CharacterController>();
    }

    public void LogicMove(Vector3 moveDir)
    {
        curMove = moveDir * 5;
    }

    public void Destroy()
    {
        GameObject.DestroyImmediate(characterController.gameObject);
    }
}
